<template>
  <view class="page pl-20 pr-20">
    <uni-card>
      <view class="inforClass">基础信息:</view>
      <view class="w100 rowsb rowsm h-100 ">
        <view style="width: 25%" class="fs-28 col333 fw-b"><text style="color: red">*</text>实践课程: </view>
        <uni-data-select style="flex: 1" v-model="practiceData.practiceType" :localdata="questionTab"></uni-data-select>
      </view>
      <view class="w100 rowsb rowsm h-100 ">
        <view style="width: 25%" class="fs-28 col333 fw-b"><text style="color: red">*</text>试卷名称：</view>
        <input style="flex: 1" class="paperName" placeholder="请输入试卷名称" placeholder-class="fs-24 col888"
          v-model="practiceData.name">
      </view>
    </uni-card>

    <uni-card padding="0">
      <view class="inforClass pt-20">面向对象</view>
      <view class="typeWarp pl-20 pr-20">
        <view class="practiceType border-botm">
          <view class="fs-28 col333 fw-b"><text style="color: red">*</text>面向学院：</view>
          <view class="option">
            <g-picker v-model="proxyName" :columns="proxyList" @confirm="changeProxy"></g-picker>
          </view>
        </view>
      </view>
      <view class="typeWarp pl-20 pr-20">
        <view class="practiceType border-botm">
          <view class="fs-28 col333 fw-b"><text style="color: red">*</text>面向专业：</view>
          <view class="option">
            <g-picker v-model="prozyName" :columns="prozyList" @confirm="changeProzy"></g-picker>
          </view>
        </view>
      </view>
      <view class="typeWarp pl-20 pr-20">
        <view class="practiceType border-botm">
          <view class="fs-28 col333 fw-b"><text style="color: red">*</text>面向年级：</view>
          <view class="option">
            <g-picker v-model="pronjName" :columns="pronjAddList" @confirm="changePronj"></g-picker>
          </view>
        </view>
      </view>
      <view class="typeWarp pl-20 pr-20">
        <view class="practiceType">
          <view class="fs-28 col333 fw-b"><text style="color: red">*</text>面向班级：</view>
          <view class="option">
            <g-picker v-model="probjName" :columns="probjList" @confirm="changeProbj"></g-picker>
          </view>
        </view>
      </view>
    </uni-card>

    <uni-card padding="0">
      <view class="inforClass">试题设置</view>

      <view class="w100 rowsb rowsm h-100 ">
        <view style="width: 25%" class="fs-28 col333 fw-b"><text style="color: red">*</text>考试时长：</view>
        <input class="paperName" type="number" placeholder="请设置考试时长(分钟)" placeholder-class="fs-24 col888"
          v-model.number="practiceData.allTime">

      </view>
      <view class="w100 h-100 rows rowsm">
        <view style="width: 25%" class="fs-28 col333 fw-b"><text style="color: red">*</text>考试时间：</view>
        <uni-datetime-picker style="flex: 1" v-model="practiceData.range" type="daterange" />
      </view>
      <view class="w100 h-100 rows rowsm">
        <view style="width: 25%" class="fs-28 col333 fw-b"><text style="color: red; visibility: hidden;">*</text>所属学期：</view>
        <uni-data-select v-if="semesterList.length > 0" style="flex: 1" v-model="practiceData.semesterConfigId"
          :localdata="semesterList" :clear="false"></uni-data-select>
        <view v-else style="flex: 1"> 请选择考试时间 </view>
      </view>
      <view class="rows rowsm h-100">
        <text class="fs-28 fw-500">单选题抽取</text>
        <input class="Select" placeholder-class="fs-24 col888" v-model.number="practiceData.selectNumber">
        <text class="fs-28 fw-500">道，</text>
        <text class="fs-28 fw-500">每道</text>
        <input class="Select" placeholder-class="fs-24 col888" v-model.number="practiceData.selectScore">
        <text class="fs-28 fw-500">积分；</text>
      </view>
      <view class="rows rowsm h-100 ">
        <text class="fs-28 fw-500">判断题抽取</text>
        <input class="Select" placeholder-class="fs-24 col888" v-model.number="practiceData.judgeNumber">
        <text class="fs-28 fw-500">道，</text>
        <text class="fs-28 fw-500">每道</text>
        <input class="Select" placeholder-class="fs-24 col888" v-model.number="practiceData.judgeScore">
        <text class="fs-28 fw-500">积分；</text>
      </view>
      <view class="rows rowsm h-100">
        <text class="fs-28 fw-500">多选题抽取</text>
        <input class="Select" placeholder-class="fs-24 col888" v-model.number="practiceData.multipleNumber">
        <text class="fs-28 fw-500">道，</text>
        <text class="fs-28 fw-500">每道</text>
        <input class="Select" placeholder-class="fs-24 col888" v-model.number="practiceData.multipleScore">
        <text class="fs-28 fw-500">积分；</text>
      </view>
      <view class="fs-26 pb-10">
        <text class="fs-28 fw-500 mr-10"> 总计: </text>
        一共{{ practiceData.selectNumber + practiceData.judgeNumber + practiceData.multipleNumber }}
        道题目，
        {{
          (practiceData.selectNumber * practiceData.selectScore) + (practiceData.judgeNumber * practiceData.judgeScore) +
          (practiceData.multipleNumber * practiceData.multipleScore)
        }}
        积分。
      </view>
    </uni-card>

    <view class="rowsa rowsm ">
      <view class="w-402 h-84 br-42 rowsc rowsm colfff mt-10"
        style="background: linear-gradient(to bottom,#4897FF,#4878FF);" @click="publishBtn">组卷</view>
    </view>
  </view>
</template>
<script>
export default {
  data() {
    return {
      practiceData: {
        practiceType: '',
        name: '',
        faceXy: null,
        faceZy: null,
        faceBj: null,
        faceNj: null,
        semesterConfigId: null,
        score: 0,
        selectNumber: 0,
        selectScore: 0,
        judgeNumber: 0,
        judgeScore: 0,
        multipleNumber: 0,
        multipleScore: 0,
        allTime: ''
      },
      questionTab: [],
      proxyList: [],
      prozyList: [],
      probjList: [],
      pronjAddList: [],
      permissList: [],
      proxyName: '',
      prozyName: '',
      probjName: '',
      pronjName: '',
      oldProxy: [999999],
      oldProzy: [99999999],
      semesterList: [],
    }
  },
  watch: {
    'practiceData.range': {
      handler(newVal) {
        if (newVal.length > 0) {
          this.getSemes(newVal);
        } else {
          this.semesterList = [];
          this.practiceData.semesterConfigId = null;
        }
      },
      deep: true
    },
  },
  onLoad(e) {
    this.getListProxy();
    this.getListProzy();
    this.getListProbj();
    this.getListPronj();
    this.getTypeCongfigList();
    // this.permissList = uni.getStorageSync('getInfo').practiceTypes;
    // const typeMap = {
    //   1: { value: '1', text: '劳动教育' },
    //   2: { value: '2', text: '职业素养' },
    //   3: { value: '3', text: '应急安全' },
    //   4: { value: '4', text: '思政实践' },
    //   5: { value: '5', text: '创新创业' }
    // };

    // this.permissList.forEach(v => {
    //   if (typeMap[v]) {
    //     this.questionTab.push(typeMap[v]);
    //   }
    // });
  },
  methods: {
    getTypeCongfigList() {
      this.$tools.axiosFromToken('GET', 'project/practice/type/config/list?pageNum=1&pageSize=50', {}, '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg)
        res.rows.forEach(item => {
          this.questionTab.push({ text: item.name, value: item.id })
        })
      })
    },
    publishBtn() {
      if (!this.practiceData.faceXy) {
        let faceXy = [];
        this.proxyList.slice(1).forEach(id => faceXy.push(id.id))
        this.practiceData.faceXy = faceXy.join(",");
      }
      if (!this.practiceData.faceZy) {
        let faceZy = [];
        this.prozyList.slice(1).forEach(id => faceZy.push(id.id))
        this.practiceData.faceZy = faceZy.join(",");
      }
      if (!this.practiceData.faceBj) {
        let faceBj = [];
        this.probjList.slice(1).forEach(id => faceBj.push(id.id))
        this.practiceData.faceBj = faceBj.join(",");
      }
      if (!this.practiceData.faceNj) {
        let faceNj = [];
        this.pronjAddList.slice(1).forEach(id => faceNj.push(id.name))
        this.practiceData.faceNj = faceNj.join(",");
      }
      if (!this.practiceData.practiceType) return this.$tools.showtt("请选择实践课程")
      if (!this.practiceData.name) return this.$tools.showtt("试卷名称不得为空")
      if (!this.practiceData.allTime) return this.$tools.showtt("考试时长不得为空或者0")

      if (this.practiceData.selectNumber == 0 && this.practiceData.judgeNumber == 0 && this.practiceData.multipleNumber == 0) return this.$tools.showtt("至少设置一种类型试题")

      if (this.practiceData.selectNumber > 0 && this.practiceData.selectScore == 0) return this.$tools.showtt("单选题题分至少大于0")
      if (this.practiceData.judgeNumber > 0 && this.practiceData.judgeScore == 0) return this.$tools.showtt("判断题题分至少大于0")
      if (this.practiceData.multipleNumber > 0 && this.practiceData.multipleScore == 0) return this.$tools.showtt("多选题题分至少大于0")
      if (!this.practiceData.range || this.practiceData.range.length == 0) return this.$tools.showtt("考试时间不得为空")
      if (this.probjList.length < 2) return this.$tools.showtt("暂无查询班级信息，请先添加班级!")
      if (!this.practiceData.faceXy) return this.$tools.showtt("请选择学院")
      if (!this.practiceData.faceZy) return this.$tools.showtt("请选择专业")
      if (!this.practiceData.faceBj) return this.$tools.showtt("请选择班级")
      if (!this.practiceData.faceNj) return this.$tools.showtt("请选择年级")

      this.practiceData.startTime = this.practiceData.range[0] + " 00:00:00"
      this.practiceData.endTime = this.practiceData.range[1] + " 23:59:59"
      this.practiceData.score = (this.practiceData.selectNumber * this.practiceData.selectScore) + (this.practiceData.judgeNumber * this.practiceData.judgeScore) +
        (this.practiceData.multipleNumber * this.practiceData.multipleScore);
      this.$tools.axiosFromToken('POST', 'project/paper/generatePaper', { ...this.practiceData }, '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg)
        // setTimeout(()=>{
        // 	this.$tools.showtt('组卷成功');
        // }, 200)
        // this.submit(res.dat);
        // uni.navigateBack({
        //   delta: 1
        // });
        this.$tools.goNext(`/teacher/trivia/analyze?type='add'&id=${res.data}&selectScore=${this.practiceData.selectScore}&judgeScore=${this.practiceData.judgeScore}&multipleScore=${this.practiceData.multipleScore}`)
      })
    },

    changeProxy(x, y, z) {
      if (JSON.stringify(this.oldProxy) === JSON.stringify(y)) return;
      this.oldProxy = y;
      let allId = [];
      if (y.find(v => v == 999999)) {
        this.proxyList.slice(1).forEach(id => allId.push(id.id))
      } else {
        allId = y;
      }
      this.practiceData.faceXy = allId.join(",");
      this.prozyList = [];
      this.probjList = [];
      if (allId.length > 0) {
        this.getListProzy(allId);
      }
    },
    changeProzy(x, y, z) {
      if (JSON.stringify(this.oldProzy) === JSON.stringify(y)) return;
      this.oldProzy = y;
      let allId = [];
      if (y.find(v => v == 99999999)) {
        this.prozyList.slice(1).forEach(id => allId.push(id.id))
      } else {
        allId = y;
      }
      this.practiceData.faceZy = allId.join(",");
      this.probjList = [];
      if (allId.length > 0) {
        this.getListProbj(allId);
      }
    },
    changeProbj(x, y, z) {
      let allId = [];
      if (y.find(v => v == 9999999999)) {
        this.probjList.slice(1).forEach(id => allId.push(id.id))
      } else {
        allId = y;
      }
      this.practiceData.faceBj = allId.join(",");
    },
    changePronj(x, y, z) {
      let allId = [];
      if (y.find(v => v == 999999999999)) {
        this.pronjAddList.slice(1).forEach(id => allId.push(id.name))
      } else {
        allId = z;
      }
      this.practiceData.faceNj = allId.join(",");
      if (this.prozyList.length > 1) {
        let zyIdList = [];
        this.prozyList.slice(1).forEach(id => zyIdList.push(id.id))
        this.getListProbj(this.practiceData.faceZy == '' || !this.practiceData.faceZy ? zyIdList : this.practiceData.faceZy.split(','));
      }
    },
    // 查询学院
    getListProxy() {
      this.$tools.axiosFromToken('GET', 'project/proxy/listTotal', {}, '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg)
        // res.rows.forEach(item => {
        // 	this.proxyList.push({ text: item.name, value: item.id })
        // })
        this.proxyList = res.rows;
        if (res.rows.length > 0) {
          this.proxyList.unshift({ name: '全部', id: 999999, check: true });
        }
      })
    },
    // 查询专业
    getListProzy(id) {
      this.prozyList = [];
      const data = {};
      if (id) {
        data.xyIds = id;
      }
      this.$tools.axiosFromToken('GET', 'project/prozy/listTotal', data, '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg)
        //  if(res.rows.length != this.prozyList.length) {
        // this.prozyList = [];
        // res.rows.forEach(item => {
        // 	this.prozyList.push({ text: item.name, value: item.id })
        // })
        //  }
        res.rows.forEach(v => this.prozyList.push(v));
        if (res.rows.length > 0) {
          this.prozyList.unshift({ name: '全部', id: 99999999, check: true });
          let bjIdList = [];
          res.rows.forEach(id => bjIdList.push(id.id))
          this.getListProbj(this.practiceData.faceZy == '' || !this.practiceData.faceZy ? bjIdList : this.practiceData.faceZy.split(','))
        }
      })
    },
    // 查询班级
    getListProbj(id) {
      let data = {};
      this.probjList = [];
      // if (id) {
      data.zyIds = id;
      data.njIds = this.practiceData.faceNj;
      // }
      if (id && data.njIds == undefined) {
        data.zyIds = id;
      }
      if (!data.zyIds) {
        delete data.zyIds;
      }
      if (!data.njIds) {
        delete data.njIds;
      }
      this.$tools.axiosFromToken('GET', 'project/probj/listTotal', data, '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg)
        // if(res.rows.length != this.probjList.length) {
        //   res.rows.forEach(item => {
        // 	this.probjList.push({ text: item.name, value: item.id })
        //   })
        // }

        this.probjList = res.rows;
        if (res.rows.length > 0) {
          this.probjList.unshift({ name: '全部', id: 9999999999, check: true });
        }
      })
    },
    getListPronj() {
      this.$tools.axiosFromToken('GET', 'project/class/selectorForList', {}, '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg)
        this.pronjAddList = res.rows;
        if (res.rows.length > 0) {
          this.pronjAddList.unshift({ name: '全部', id: 999999999999, check: true });
        }
      })
    },
    getZyList(e) {
      if (e) {
        this.getListProzy(e);
      }
    },
    getBjList(e) {
      if (e) {
        this.getListProbj(e);
      }
    },

    getSemes(e) {
      this.semesterList = [];
      this.$tools.axiosFromToken('GET', 'project/semester/config/list', { startTime: e[0] + ' 00:00:00' }, '加载中').then((res) => {
        if (res.code == 200) {
          const semesterList = res.rows.filter(item => {
            if (
              (new Date(e[1] + ' 23:59:59').getTime() >= new Date(item.startTime).getTime() && new Date(e[1] + ' 23:59:59').getTime() <= new Date(item.endTime).getTime())
              ||
              (new Date(e[0] + ' 00:00:00').getTime() <= new Date(item.startTime).getTime() && new Date(e[1] + ' 23:59:59').getTime() <= new Date(item.endTime).getTime() && new Date(e[1] + ' 23:59:59').getTime() >= new Date(item.startTime).getTime())
              ||
              (new Date(e[0] + ' 00:00:00').getTime() >= new Date(item.startTime).getTime() && new Date(e[1] + ' 23:59:59').getTime() >= new Date(item.endTime).getTime())
            ) {
              return item;
            }
          });
          this.$nextTick(() => {
            this.semesterList = semesterList.map(item => ({ value: String(item.id), label: item.name, text: item.name }));
            this.practiceData.semesterConfigId = this.semesterList.length > 0 ? this.semesterList[0].value : null;
          });

        }
      });
    },
    submit(id) {
      this.$tools.axiosFromToken('PUT', `project/paper`, { status: 1, id }, '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg);
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.page {
  width: 100%;
  background: #f4f5f7;
  padding-bottom: 10rpx;

  ::v-deep .uni-select__input-box {
    min-height: 32px !important;
  }

  ::v-deep .uni-date__x-input {
    height: auto !important;
    padding: 5px !important;
  }
}

::v-deep .uni-date__icon-clear {
  border: 0 !important;
  top: 2px !important;
}

::v-deep .uni-card {
  margin-left: 5px !important;
  margin-right: 5px !important;
  overflow: visible;
}

::v-deep .uni-card__content {
  color: #000 !important;
}

.practiceType {
  width: 100%;
  display: flex;
  align-items: center;
  // justify-content: space-between;
  height: 100rpx;

}

::v-deep .uni-stat__select {
  padding-left: 0 !important;
  padding-right: 0 !important;
}

.paperName {
  flex: 1;
  height: 35px;
  border-radius: 3px;
  text-align: left;
  padding: 0 5px;
  border: 1px solid #ededed;
  // margin-left: 15px;
}

.Select {
  width: 76rpx;
  height: auto;
  border-radius: 3px;
  text-align: center;
  padding: 6rpx;
  border: 1px solid #adacac;
  margin: 0 10px;
}

::v-deep .uni-stat__select {
  padding-right: 0 !important;

  uni-view {
    width: 100% !important;
  }
}

.inforClass {
  font-size: 16px;
  font-weight: 700;
  display: flex;
  align-items: center;
  margin: 8px 0;
}

.inforClass::before {
  content: '';
  width: 5px;
  height: 20px;
  background-color: #007AFF;
  margin-right: 5px;
  display: inline-block;
}

.practiceType {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 90rpx;
}

.border-botm {
  border-bottom: 1px #f1f1f1 solid;
}
</style>